package com.justplay.mobilelocation;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.justplay.mobilelocation.UI.ContactUsActivity;
import com.justplay.mobilelocation.UI.ParameterSettingActivity;
import com.justplay.mobilelocation.UI.SoftwarAccreditCodeActivity;
import com.justplay.mobilelocation.UI.UserAgreementActivity;
import com.justplay.mobilelocation.util.InputActivityUtil;
import com.justplay.mobilelocation.util.SharePreferencesUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.justplay.mobilelocation.util.Config.shouquanma;


/**
 * A simple {@link Fragment} subclass.
 */
public class SettingFragment extends Fragment {


    @Bind(R.id.location)
    ImageView location;
    @Bind(R.id.tv_AuthorizationCode)
    TextView tv_AuthorizationCode;
    @Bind(R.id.tv_parameter_setting)
    TextView tv_parameter_setting;
    @Bind(R.id.tv_contact)
    TextView tv_contact;
    @Bind(R.id.toolbar_title)
    TextView toolbar_title;
    @Bind(R.id.toolbar_right)
    ImageView toolbar_right;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tv_piliangkongzhi)
    TextView tv_piliangkongzhi;
    @Bind(R.id.btn_exit)
    Button btn_exit;
    @Bind(R.id.tv_userName)
    TextView tv_userName;
    @Bind(R.id.tv_state)
    TextView tv_state;

    public SettingFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_setting, container, false);
        ButterKnife.bind(this, view);
        toolbar_title.setText("设置");
        String userName = SharePreferencesUtil.getString("userName", "", getActivity());

        tv_userName.setText(userName);

        Boolean havedBuy = SharePreferencesUtil.getBoolean("havedBuy", false, getActivity());
        if (havedBuy) {
            tv_state.setText("网络未开通");
        } else {
            tv_state.setText("未输入授权码");
        }

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.location, R.id.tv_AuthorizationCode, R.id.tv_userAgreement, R.id.tv_parameter_setting, R.id.tv_contact, R.id.tv_piliangkongzhi, R.id.btn_exit, R.id.tv_gaoji, R.id.tv_modifyPasswd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_piliangkongzhi:
            case R.id.tv_gaoji:
                InputActivityUtil.gotoInputCodeActivity(getActivity());
                break;
            case R.id.location:
                break;
            case R.id.tv_AuthorizationCode:
                startActivity(new Intent(getActivity(), SoftwarAccreditCodeActivity.class));

                break;
            case R.id.tv_parameter_setting:
                startActivity(new Intent(getActivity(), ParameterSettingActivity.class));
                break;
            case R.id.tv_userAgreement:
                startActivity(new Intent(getActivity(), UserAgreementActivity.class));
                break;

            case R.id.tv_contact:
                startActivity(new Intent(getActivity(), ContactUsActivity.class));

                break;
            case R.id.btn_exit:
                SharePreferencesUtil.putString("userName", "", getActivity());
                startActivity(new Intent(getActivity(), MainActivity.class));
                getActivity().finish();
                break;
            case R.id.tv_modifyPasswd:
                InputActivityUtil.gotoInputCodeActivity(getActivity());
                break;
        }
    }

    @Override
    public void onResume() {
        Boolean havedBuy = SharePreferencesUtil.getBoolean("havedBuy", false, getActivity());
        if (havedBuy) {
            tv_state.setText("网络未开通");
            String string = SharePreferencesUtil.getString(shouquanma, "", getActivity());
            tv_AuthorizationCode.setText("软件授权码:       "+string);
//            tv_AuthorizationCode.setEnabled(false);
        } else {
            tv_state.setText("未输入授权码");
        }
        super.onResume();

    }
}
